package org.csi.controle.status;

import java.io.IOException;
import java.net.InetAddress;
import java.net.UnknownHostException;

public class LocalizaImpressora implements Runnable {

	private String host = "";

	private String ip;

	private String subnet;

	private boolean achou = false;

	public LocalizaImpressora(String subNet) {
		this.subnet = subNet;
	}

	public static void main(String[] args) {
		LocalizaImpressora li = new LocalizaImpressora("192.168.0.");
		li.run();
	}

	@Override
	public void run() {
		achou = false;
		while(!achou) {
			for(int i=1; i<254; i++){
				try {
					this.ip = subnet + String.valueOf(i);
					InetAddress inetAddress2 = InetAddress.getByName(ip);
					System.out.println(ip);
					if(inetAddress2.isReachable(2000)){
						System.out.println("OK           "+ip);
						this.host = inetAddress2.getHostName();
						System.out.println("IA: "+inetAddress2);

						System.out.println(this.host);
						if(this.host.toUpperCase().contains("BEMATECH")) {
							achou = true;
							break;
						}
					}
				} catch (UnknownHostException e) {
					e.printStackTrace();
				} catch (IOException e) {
					e.printStackTrace();

				}
			}	    	
		}
	}

	public String getHost() {
		return host;
	}

	public String getIp() {
		return ip;
	}

	public boolean isAchou() {
		return achou;
	}

	public void setAchou(boolean achou) {
		this.achou = achou;
	}
	
}